package org.zk.dubbo_consumer.controller;


import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.zk.entity.Comment;
import org.zk.entity.Course;
import org.zk.entity.User;
import org.zk.service.CommentService;
import org.zk.service.CourseService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author Alicelonely
 * @since 2019-12-11
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;
    @Resource
    private CourseService courseService;

    @RequestMapping("/getCommentByCid")
    public Map getCommentByCid(int courseId, int pageSize, int pageNum) {
        Map map=new HashMap();
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        map.put("courseId",courseId);

        PageInfo page=commentService.getqueryCommentPage(map);
        map.put("page",page);
        map.put("totalPage",page.getPages());
        return map;
    }

    /**
     * 添加评价
     *
     * @param
     * @param comment
     * @param course
     * @return
     */
    @RequestMapping("/addComment")
    public List addComment(Comment comment, Course course,HttpSession session) {
        //user.setUserId(1);//模拟传值

        if (comment.getCommentContent() == "") {
            comment.setCommentContent("该用户并无发表言论");
        }
        comment.setCourse(course);
        comment.setUser((User) session.getAttribute("user"));//gs修改，从session从获取的用户

        //每添加一条评论对课程表里的评价数等进行相应操作
        Course course1 = courseService.queryCourse(course.getCourseId());

        //好评
        if (comment.getCommentStatus() == 0) {
            course1.setCourseAll(course1.getCourseAll() + 1);
            course1.setCourseGood(course1.getCourseGood() + 1);
            course1.setCourseId(course.getCourseId());
            float rate = (float)course1.getCourseGood() / (float)course1.getCourseAll();
            course1.setCourseFavorateRate(rate);
            courseService.updateCourse(course1);
        }
        //差评
        if (comment.getCommentStatus() == 1) {
            course1.setCourseAll(course1.getCourseAll() + 1);
            course1.setCourseCha(course1.getCourseCha() + 1);
            course1.setCourseId(course.getCourseId());
            float rate = (float)course1.getCourseGood() / (float)course1.getCourseAll();
            course1.setCourseFavorateRate(rate);
            courseService.updateCourse(course1);
        }
        int result = commentService.addComm(comment);
        List list = new ArrayList();
        if (result > 0) {
            list.add("succ");
            return list;
        } else {
            list.add("fail");
            return list;
        }

    }

    public CommentService getCommentService() {
        return commentService;
    }

    public void setCommentService(CommentService commentService) {
        this.commentService = commentService;
    }

    public CourseService getCourseService() {
        return courseService;
    }

    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }
}

